/*
Copyright 2005 - 2017 by Paolo Brandoli/Binarno s.p.

Imebra is available for free under the GNU General Public License.

The full text of the license is available in the file license.rst
 in the project root folder.

If you do not want to be bound by the GPL terms (such as the requirement
 that your application must also be GPL), you may purchase a commercial
 license for Imebra from the Imebra’s website (http://imebra.com).
*/

#if !defined(imebraObjcDrawBitmap__INCLUDED_)
#define imebraObjcDrawBitmap__INCLUDED_

#include "imebra_macros.h"

#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIImage.h>
#else
#import <AppKit/NSImage.h>
#endif


@class ImebraImage;
@class ImebraTransform;
@class ImebraMemory;


/// \enum ImebraDrawBitmapType
/// \brief Defines the type of the bitmap generated by ImebraDrawBitmap.
///
///////////////////////////////////////////////////////////////////////////////
typedef NS_ENUM(unsigned int, ImebraDrawBitmapType)
{
    ImebraDrawBitmapTypeRGB  = 0, ///< Generates a BMP image where each pixel contains 3 bytes (R, G and B)
    ImebraDrawBitmapTypeBGR  = 1, ///< Generates a BMP image where each pixel contains 3 bytes (B, G and R)
    ImebraDrawBitmapTypeRGBA = 2, ///< Generates a BMP image where each pixel contains 4 bytes (R, G, B and A)
    ImebraDrawBitmapTypeBGRA = 3  ///< Generates a BMP image where each pixel contains 4 bytes (B, G, R and A)
};


///
/// \brief ImebraDrawBitmap takes care of converting an Image object into an
///        array of bytes that can be displayed by the operating system
///        facilities.
///
/// ImebraDrawBitmap can apply several transformations to the Image before
/// generating the bitmap.
///
/// DrawBitmap applies automatically the necessary color transform and high
/// bit shift in order to obtain a 8 bits per channel RGB image.
///
///////////////////////////////////////////////////////////////////////////////
@interface ImebraDrawBitmap: NSObject

{
    define_imebra_object_holder(DrawBitmap);
}

    /// \brief Initializes a ImebraDrawBitmap with no transforms.
    ///
    /// The getBitmap() method will not apply any transform to the Image before
    /// generating the bitmap (except for the high bit and color space to obtain
    /// a 24bpp RGB image).
    ///
    ///////////////////////////////////////////////////////////////////////////////
    -(id)init;

    /// \brief Initializes a ImebraDrawBitmap object that always apply the
    ///        transform specified in the parameter when calling getBitmap()
    ///        or getImebraImage().
    ///
    /// \param pTransform the transforms to apply to the Image in the
    ///                   getBitmap() method
    ///
    ///////////////////////////////////////////////////////////////////////////////
    -(id)initWithTransform:(ImebraTransform*)pTransform;

    -(void)dealloc;

    /// \brief Apply the transforms defined in the constructor (if any) to the
    ///        input image, then calculate an array of bytes containing a bitmap
    ///        that can be rendered by the operating system.
    ///
    /// \param pImage         the image for which the bitmap must be calculated
    /// \param drawBitmapType the type of bitmap to generate
    /// \param rowAlignBytes  the number of bytes on which the bitmap rows are
    ///                       aligned
    /// \param pError         set to a NSError derived class in case of error
    /// \return a ImebraMemory object containing the buffer containing
    ///         the generated bitmap
    ///
    ///////////////////////////////////////////////////////////////////////////////
    -(ImebraMemory*) getBitmap:(ImebraImage*)pImage bitmapType:(ImebraDrawBitmapType) drawBitmapType rowAlignBytes:(unsigned int)rowAlignBytes error:(NSError**)pError;


#if defined(__APPLE__)

    /// \brief Apply the transforms defined in the constructor (if any) to the
    ///        input image, then return a NSImage (on OS-X) or an UIImage
    ///        (on iOS).
    ///
    /// \param pImage         the image for which the NSImage or UIImage
    ///                       must be calculated
    /// \param pError         set to a NSError derived class in case of error
    /// \return a NSImage on OS-X or an UIImage on iOS
    ///
    ///////////////////////////////////////////////////////////////////////////////
#if TARGET_OS_IPHONE
    -(UIImage*)getImebraImage:(ImebraImage*)pImage error:(NSError**)pError;
#else
    -(NSImage*)getImebraImage:(ImebraImage*)pImage error:(NSError**)pError;
#endif

#endif

@end

#endif // imebraObjcDrawBitmap__INCLUDED_


